package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.UserLocationApi;
import com.tanhua.server.paramPojo.LocationInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author feihao
 * @version 1.0
 * @date 2021/7/8 16:40
 */
@Slf4j
@Service
public class BaiduService {


    @Reference(version = "1.0.0")
    private UserLocationApi userLocationApi;

    /**
     * 上报位置信息
     *
     * @param locationInfo
     * @return
     */
    public Boolean updateLocation(LocationInfo locationInfo) {

        //判断上传的信息是否为空
        if (ObjectUtil.isAllEmpty(locationInfo.getAddrStr(),locationInfo.getLatitude(),locationInfo.getLatitude())) {
            return false;
        }

        User user = UserThreadLocal.get();
        try {
            //不为空：
            return userLocationApi.updateUserLocation(user.getId(),
                    locationInfo.getLongitude(),
                    locationInfo.getLatitude(),
                    locationInfo.getAddrStr());

        } catch (Exception e) {

            log.error("更新地理位置失败~ userId = " + user.getId()
                    + ", longitude = " + locationInfo.getLongitude()
                    + ", latitude = " + locationInfo.getLatitude()
                    + ", address = " + locationInfo.getAddrStr(), e);
        }
        return false;
    }
}
